// pages/car_order/check_car/check_car.js
var api = require('../../../api/api');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgArr:[],
    content:'',
    order_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type:options.type,
      order_id:options.order_id,
      index:options.index
    })
   
  },
  appointArrive(){
    if(!this.data.content){
      wx.showToast({
        title: '请填写车辆情况',
        icon:'none'
      })
      return false;
    }
    if(this.data.imgArr.length < 1){
      wx.showToast({
        title: '请上传车况图片',
        icon:'none'
      })
      return false;
    }
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2];
    let listsIndex = "lists["+this.data.index+"].status"

    if(this.data.type == 0){ //确认到达
      api.post('/appoint/arrive',{
        order_id:this.data.order_id,
        info1:{
          images:this.data.imgArr,
          content:this.data.content
        }
      }).then(res=>{
        let optionNum1 = "tabNum[1]"
        let optionNum2 = "tabNum[2]"
        prevPage.setData({
          [listsIndex]:1,
          [optionNum1]:prevPage.data.tabNum[1] - 1,
          [optionNum2]:prevPage.data.tabNum[2] + 1,
        })
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(function(){
          wx.navigateBack({
            delta:1
          })
        },1000)
       
      })
    }else if(this.data.type == 1){ //确定完成
      api.post('/appoint/confirm',{
        order_id:this.data.order_id,
        info2:{
          images:this.data.imgArr,
          content:this.data.content
        }
      }).then(res=>{
        let optionNum2 = "tabNum[2]"
        let optionNum3 = "tabNum[3]"
        prevPage.setData({
          [listsIndex]:2,
          [optionNum2]:prevPage.data.tabNum[2] - 1,
          [optionNum3]:prevPage.data.tabNum[3] + 1,
        })
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(function(){
          wx.navigateBack({
            delta:1
          })
        },1000)
      })
    }
  },

  //从本地获取照片 
  getLocalityImg() {
    let that = this;
    if (this.data.imgArr.length < 5) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'], //可以指定原图或压缩图,默认二者都有
        sourceType: ['album', 'camera'], //指定图片来源是相机还是相册,默认二者都有
        success(res) {
          console.log(res, "---------上传的图片")

          const tempFiles = res.tempFiles[0].path //包含图片大小的数组
          wx.showLoading({
            title: '上传图片中',
            mask:true
          })
          wx.uploadFile({
            header: {
              'Content-Type': 'application/json; charset=UTF-8',
              'X-Requested-With': 'XMLHttpRequest',
              'Authorization': wx.getStorageSync('token')
            },
            filePath: tempFiles,
            name: 'file',
            url: api.httpUrl + "/common/upload",
            success: res => {
              wx.hideLoading({
                success: (res) => {},
              })
              let upload_data = res.data;
              upload_data = JSON.parse(upload_data)
        
              const upload_image = upload_data.data.url;
              console.log("upload_image", upload_image)
              let imgArr = that.data.imgArr;
              imgArr.push(upload_image)
              that.setData({
                imgArr
              })
            }
          })
                

        }
      })

    } else {
      wx.showToast({ //超过图片张数限制提示
        title: '最多上传五张图片',
        icon: 'none',
        duration: 2000
      })

    }
  },

  //删除照片功能与预览照片功能 
  deleteImg(e) {
    let that = this;
    let imgArr = that.data.imgArr;
    let index = e.currentTarget.dataset.index; //获取长按删除图片的index
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success(res) {
        if (res.confirm) {
          // console.log('点击确定了');
          imgArr.splice(index, 1);
        } else if (res.cancel) {
          // console.log('点击取消了');
          return false;
        }
        that.setData({
          imgArr: imgArr
        });
      }
    })
  },

  //预览图片
  previewImg(e) {
    let index = e.currentTarget.dataset.index;
    let imgArr = this.data.imgArr;
    wx.previewImage({
      current: imgArr[index],
      urls: imgArr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})